/*
 * @Author: xiaosihan
 * @Date: 2024-08-18 18:23:32
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-10-01 23:51:11
 */

import { observable, configure, toJS } from "mobx";
configure({ enforceActions: "never" });

const dragModelStore = observable(
  {
    showList: [] as Array<string>,
    setShowList(id: string) {
      if (this.showList.includes(id)) {
        this.showList = this.showList.filter((l) => l !== id);
      } else {
        this.showList = [...this.showList, id];
      }
    },
    close(...ids: Array<string>) {
      this.showList = this.showList.filter((l) => !ids.includes(l));
    },

    positionList: JSON.parse(localStorage.positionList || "[]") as Array<{
      id: string;
      left: number;
      top: number;
    }>,
    setPositionList(conf: { id: string; left: number; top: number }) {
      let positionList = this.positionList.find((p) => p.id === conf.id);
      if (positionList) {
        Object.assign(positionList, conf);
      } else {
        this.positionList.push(conf);
      }
      this.positionList = [...this.positionList];
      localStorage.positionList = JSON.stringify(this.positionList);
    },
    getPositionList(id: string) {
      let positionList = this.positionList.find((p) => p.id === id);
      if (!positionList) {
        return undefined;
      }
      return toJS(positionList);
    },
    // 关闭全部
    closeAll() {
      this.showList = [];
    },
  },
  {},
  { deep: false }
);

export default dragModelStore;
